package com.ml.mall.service.seting.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ml.mall.bean.base.ApiRestResult;
import com.ml.mall.bean.req.seting.SetingEditDto;
import com.ml.mall.bean.req.seting.SetingNewDto;
import com.ml.mall.bean.res.seting.CountryRes;
import com.ml.mall.bean.res.seting.SetingRes;
import com.ml.mall.entity.country.Country;
import com.ml.mall.entity.seting.SysASeting;
import com.ml.mall.entity.seting.SysASetingCurrency;
import com.ml.mall.entity.seting.SysASetingLanguage;
import com.ml.mall.entity.seting.SysASetingTime;
import com.ml.mall.enums.CommonEnum;
import com.ml.mall.exception.BizException;
import com.ml.mall.mapper.country.CountryMapper;
import com.ml.mall.mapper.seting.SysASetingCurrencyMapper;
import com.ml.mall.mapper.seting.SysASetingLanguageMapper;
import com.ml.mall.mapper.seting.SysASetingMapper;
import com.ml.mall.mapper.seting.SysASetingTimeMapper;
import com.ml.mall.service.seting.SysASetingService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class SysASetingServiceImpl extends ServiceImpl<SysASetingMapper, SysASeting> implements SysASetingService{

    @Autowired
    private SysASetingMapper sysASetingMapper;
    @Autowired
    private SysASetingLanguageMapper sysASetingLanguageMapper;
    @Autowired
    private SysASetingCurrencyMapper sysASetingCurrencyMapper;
    @Autowired
    private SysASetingTimeMapper sysASetingTimeMapper;
    @Autowired
    private CountryMapper countryMapper;

    @Override
    public ApiRestResult createSeting(SetingNewDto dto) {
        SysASeting sysASeting = new SysASeting();
        BeanUtils.copyProperties(dto,sysASeting);
        int isAdd = sysASetingMapper.insert(sysASeting);
        if(isAdd < 1){
            throw new BizException(CommonEnum.INSERT_ERROR);
        }
        return ApiRestResult.ok();
    }

    @Override
    public ApiRestResult editSeting(SetingEditDto dto) {
        SysASeting sysASeting = new SysASeting();
        BeanUtils.copyProperties(dto,sysASeting);
        int isUpdate = sysASetingMapper.updateById(sysASeting);
        if(isUpdate < 1){
            throw new BizException(CommonEnum.UPDATE_ERROR);
        }
        return ApiRestResult.ok();
    }

    @Override
    public List<SetingRes> queryListSeting() {
        List<SysASeting> setingList = sysASetingMapper.selectList(null);
        List<SetingRes> setingResList = new ArrayList<>();
        for (SysASeting seting:setingList){
            SetingRes setingRes = new SetingRes();
            BeanUtils.copyProperties(seting,setingRes);
            List<SysASetingLanguage> languageList = sysASetingLanguageMapper.selectList(null);
            setingRes.setLanguageArr(languageList);
            List<SysASetingCurrency> currencyList = sysASetingCurrencyMapper.selectList(null);
            setingRes.setCurrencyArr(currencyList);
            List<SysASetingTime> timeList = sysASetingTimeMapper.selectList(null);
            setingRes.setTimeArr(timeList);
            List<CountryRes> countryList = countryMapper.selectListCountry();
            setingRes.setCountryArr(countryList);
            setingResList.add(setingRes);
        }
        return setingResList;
    }
}
